﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using TomanuExtensions;

namespace Raytracer.Configurations
{
    public class LastOpenFiles
    {
        private const int MAX_RECENT = 10;

        private string m_image_last_open_dir = "";
        private List<string> m_recent_files = new List<string>();
        private string m_last_open_file = "";
        private string m_last_open_dir = "";

        internal LastOpenFiles()
        {
        }

        internal LastOpenFiles(XElement a_node)
        {
            m_image_last_open_dir = a_node.Element("ImageLastOpenDir").Value;
            m_recent_files = (from n in a_node.Element("RecentFiles").Elements()
                              select n.Value).ToList();
            m_last_open_file = a_node.Element("LastOpenFile").Value;
            m_last_open_dir = a_node.Element("LastOpenDir").Value;
            Check();
        }

        internal XElement GetAsXml()
        {
            return new XElement("LastOpenFiles",
                new XElement("ImageLastOpenDir", m_image_last_open_dir),
                new XElement("LastOpenFile", m_last_open_file),
                new XElement("LastOpenDir", m_last_open_dir),
                new XElement("RecentFiles",
                    from rf in m_recent_files
                    select new XElement("RecentFile", rf)));
        }

        public void Opened(string a_file)
        {
            m_recent_files.Remove(a_file);
            m_recent_files.Insert(0, a_file);
            m_recent_files = m_recent_files.Take(MAX_RECENT).ToList();

            m_last_open_file = a_file;
            m_last_open_dir = Path.GetDirectoryName(a_file);

            Check();
            Configuration.Instance.Save();
        }

        public IEnumerable<string> RecentFiles
        {
            get
            {
                return m_recent_files;
            }
        }

        public string LastOpenFile
        {
            get
            {
                return m_last_open_file;
            }
        }

        public string ImageLastOpenDir
        {
            get
            {
                return m_image_last_open_dir;
            }
            set
            {
                m_image_last_open_dir = value;

                Check();
                Configuration.Instance.Save();
            }
        }

        public string LastOpenDir
        {
            get
            {
                return m_last_open_dir;
            }
        }

        private string CheckFile(string a_file)
        {
            try
            {
                if (a_file == "")
                    return string.Empty;

                if (!new FileInfo(a_file).Exists)
                    return string.Empty;

                return a_file;
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }

        private string CheckDirectory(string a_fileName)
        {
            try
            {
                if (a_fileName == "")
                    return string.Empty;

                return new DirectoryInfo(a_fileName).FindExistingDirectory();
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public void Check()
        {
            for (int i = 0; i < m_recent_files.Count; i++)
            {
                try
                {
                    if (m_recent_files[i] != "")
                        if (!new FileInfo(m_recent_files[i]).Exists)
                            m_recent_files[i] = "";
                }
                catch (Exception)
                {
                    m_recent_files[i] = "";
                }
            }

            m_recent_files = (from s in
                                  (from s in m_recent_files
                                   select s.Trim())
                              where !String.IsNullOrEmpty(s)
                              group s by s into g
                              select g.First()).ToList();

            m_image_last_open_dir = CheckDirectory(ImageLastOpenDir);
            m_last_open_file = CheckFile(m_last_open_file);
            m_last_open_dir = CheckDirectory(m_last_open_dir);
        }
    }
}